package com.ydx.meituan.model.Annotations;


import com.ydx.meituan.utils.ParamsCheckUtil;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 *
 * Description:非空校验
 *
 * */
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = YdxNotBlankValidator.class)
public @interface YdxNotBlank {

    String message() default "";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}
class YdxNotBlankValidator implements ConstraintValidator<YdxNotBlank,Object> {

    public String PARAMS_NAME="";

    @Override
    public void initialize(YdxNotBlank constraintAnnotation) {
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        PARAMS_NAME  = ParamsCheckUtil.getParamsName(constraintValidatorContext);
        constraintValidatorContext.disableDefaultConstraintViolation();
        if (value==null){
            constraintValidatorContext.buildConstraintViolationWithTemplate(PARAMS_NAME + "不能为空").addConstraintViolation();
            return false;
        }

        if (value instanceof String){
            String v=value.toString();
            if ("".equals(v.trim())){
                constraintValidatorContext.buildConstraintViolationWithTemplate(PARAMS_NAME + "不能为空").addConstraintViolation();
                return false;
            }
        }

        return true;
    }
}